#ifndef ENTRANCEVIEW_HPP
#define ENTRANCEVIEW_HPP

#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QDateTimeEdit>
#include <QLabel>
#include <QDebug>
#include <QDateEdit>
#include <QTimeEdit>
#include <QComboBox>
#include <QSpacerItem>
#include "assembly/viewmodel/EntranceViewModel.hpp"
#include "assembly/widget/SecondsOnlyTimeEdit.hpp"

class EntranceView : public QWidget {
    Q_OBJECT

public:
    explicit EntranceView(QWidget *parent = nullptr)
        : QWidget(parent) {
        viewModel = new EntranceViewModel(this);
        oViewUI();
        oConnections();
    }

private:
    void oViewUI() {
        layout = new QVBoxLayout(this);
        layout->setSpacing(10);  // 调整间距，避免控件过于拥挤

        // 创建顶部容器并设置样式
        topContainer = new QWidget(this);
        topContainer->setStyleSheet("background-color: white;");
        topLayout = new QHBoxLayout(topContainer);
        topContainer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

        // 创建按钮布局（水平布局）
        QHBoxLayout *buttonLayout = new QHBoxLayout();
        rhythmButton = new QPushButton("节拍图", this);
        timeSequenceButton = new QPushButton("时序图", this);
        buttonLayout->addWidget(rhythmButton);
        buttonLayout->addWidget(timeSequenceButton);

        // 将按钮布局添加到 topLayout
        topLayout->addLayout(buttonLayout);  // 水平布局

        // 设置日期时间选择框布局
        dateTimeLayout = new QHBoxLayout();

        // 选择日期
        selectDateLabel = new QLabel("选择日期:", this);
        selectDateComboBox = new QComboBox(this);  // 下拉组件

        // 从 viewModel 获取日期数据
        QStringList dates = viewModel->getDates();
        selectDateComboBox->addItems(dates);

        selectDateComboBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);

        // 开始时间和结束时间
        startTimeEdit = new SecondsOnlyTimeEdit(this);
        endTimeEdit = new SecondsOnlyTimeEdit(this);

        // 设置初始时间
        QTime initialStartTime = viewModel->getStartTime().time();
        QTime initialEndTime = viewModel->getEndTime().time();

        startTimeEdit->setTime(initialStartTime);
        startTimeEdit->setMinimumTime(QTime(0, 0, 0)); // 可根据需求调整
        startTimeEdit->setMaximumTime(initialEndTime);

        endTimeEdit->setTime(initialEndTime);
        endTimeEdit->setMinimumTime(initialStartTime);
        endTimeEdit->setMaximumTime(QTime(23, 59, 59)); // 可根据需求调整

        QSize fixedSize(dates.size() > 0 ? 120 + dates.first().size() / 2 : 120, 32);
        QSize timeEditSize(100, 32);
        selectDateComboBox->setFixedSize(fixedSize);
        selectDateComboBox->setStyleSheet("QComboBox { color: black; }");
        startTimeEdit->setFixedSize(timeEditSize);
        endTimeEdit->setFixedSize(timeEditSize);

        // 设置水平布局
        dateTimeLayout->addWidget(selectDateLabel);
        dateTimeLayout->addWidget(selectDateComboBox);
        dateTimeLayout->addWidget(startTimeEdit);
        dateTimeLayout->addWidget(endTimeEdit);

        // 隐藏时间编辑框的初始状态
        startTimeEdit->hide();
        endTimeEdit->hide();

        // 设置布局居中对齐
        dateTimeLayout->setAlignment(Qt::AlignCenter);  // 将布局中的控件居中对齐

        // 添加右对齐的间隔和日期时间布局
        QSpacerItem* spacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
        topLayout->addSpacerItem(spacer);  // 添加右侧空间
        topLayout->addLayout(dateTimeLayout);  // 添加日期时间布局到最右

        // 创建底部区域布局
        bottomContainer = new QWidget(this);
        bottomContainer->setStyleSheet("background-color: white;");
        bottomLayout = new QVBoxLayout(bottomContainer);
        bottomContainer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

        chartLabel = new QLabel("请选择图表类型", this);
        bottomLayout->addWidget(chartLabel);  // 初始显示的内容

        layout->addWidget(topContainer);  // 将 topContainer 添加到主布局
        layout->addWidget(bottomContainer);  // 将 bottomContainer 添加到主布局
    }

    void oConnections() {
        // 连接信号和槽
        connect(rhythmButton, &QPushButton::clicked, this, [this]() {
            viewModel->setChartType(EntranceModel::ChartType::Rhythm);
        });
        connect(timeSequenceButton, &QPushButton::clicked, this, [this]() {
            viewModel->setChartType(EntranceModel::ChartType::TimeSequence);
        });

        // 连接时间编辑控件的信号
        connect(startTimeEdit, &QTimeEdit::timeChanged, this, [this](const QTime &time) {
            viewModel->setStartTime(selectDateComboBox->currentText(), time);
        });

        connect(endTimeEdit, &QTimeEdit::timeChanged, this, [this](const QTime &time) {
            viewModel->setEndTime(selectDateComboBox->currentText(), time);
        });

        // 连接日期选择框的信号
        connect(selectDateComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int index) {
            QString selectedDate = selectDateComboBox->itemText(index);
            viewModel->setSelectedDate(selectedDate);
        });

        // 连接视图模型信号
        connect(viewModel, &EntranceViewModel::chartTypeChanged, this, &EntranceView::updateChart);
        connect(viewModel, &EntranceViewModel::startTimeChanged, this, &EntranceView::updateStartTime);
        connect(viewModel, &EntranceViewModel::endTimeChanged, this, &EntranceView::updateEndTime);
    }

private slots:
    void updateChart(EntranceModel::ChartType type) {
        // 清空底部布局
        QLayoutItem* item;
        while ((item = bottomLayout->takeAt(0)) != nullptr) {
            if (item->widget()) {
                delete item->widget();  // 删除现有的 widget
            }
            delete item;
        }

        // 调用 viewModel 中的 createChartWidget 来创建图表
        QWidget *chartWidget = viewModel->createChartWidget(type, this);

        // 添加到布局中
        if (chartWidget) {
            bottomLayout->addWidget(chartWidget);
        }
    }

    void updateStartTime(const QDateTime &startTime) {
        // 仅更新开始时间编辑控件的时间
        QTime newTime = startTime.time();
        if (startTimeEdit->time() != newTime) {
            startTimeEdit->blockSignals(true); // 防止信号触发
            startTimeEdit->setTime(newTime);
            startTimeEdit->blockSignals(false);
        }

        // 更新 endTimeEdit 的最小时间
        endTimeEdit->setMinimumTime(newTime);
    }

    void updateEndTime(const QDateTime &endTime) {
        // 仅更新结束时间编辑控件的时间
        QTime newTime = endTime.time();
        if (endTimeEdit->time() != newTime) {
            endTimeEdit->blockSignals(true); // 防止信号触发
            endTimeEdit->setTime(newTime);
            endTimeEdit->blockSignals(false);
        }

        // 更新 startTimeEdit 的最大时间
        startTimeEdit->setMaximumTime(newTime);
    }

private:
    EntranceViewModel *viewModel;
    QVBoxLayout *layout;
    QWidget *topContainer;
    QHBoxLayout *topLayout;
    QHBoxLayout *dateTimeLayout;
    QLabel *selectDateLabel;
    QComboBox *selectDateComboBox;
    QTimeEdit *startTimeEdit;
    QTimeEdit *endTimeEdit;

    QWidget *bottomContainer;
    QVBoxLayout *bottomLayout;
    QLabel *chartLabel;

    QPushButton *rhythmButton;
    QPushButton *timeSequenceButton;
};

#endif // ENTRANCEVIEW_HPP
